/**
 * Copyright (C) 2010 David Simonek <dafesimonek@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.dafe.partyband.gui.songs;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.vfs.FileChangeEvent;
import org.apache.commons.vfs.FileListener;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.VFS;
import org.apache.commons.vfs.impl.DefaultFileMonitor;
import org.dafe.partyband.db.api.ISongResource;
import org.dafe.partyband.db.api.handlers.ISongResourceHandler;
import org.dafe.partyband.db.api.DbManager;
import org.openide.util.Exceptions;

/**
 *
 * @author dafe
 */
public class ExternalModificationsSynchronizer {

    private static final ISongResourceHandler resHandler = DbManager.getBandManager().getHandlersLookup().lookup(ISongResourceHandler.class);

    private DefaultFileMonitor fm;
    private Map<File, ISongResource> file2SongRes = new HashMap<File, ISongResource>();
    private Map<FileObject, File> fo2File = new HashMap<FileObject, File>();

    public ExternalModificationsSynchronizer() {
        fm = new DefaultFileMonitor(new FileListener() {

            public void fileCreated(FileChangeEvent fce) throws Exception {
                // noop
            }

            public void fileDeleted(FileChangeEvent fce) throws Exception {
                // noop
            }

            public void fileChanged(FileChangeEvent fce) throws Exception {
                File file = fo2File.get(fce.getFile());
                ISongResource songRes = file2SongRes.get(file);
                updateSongResFromFile(songRes, file);
            }
        });
        fm.setRecursive(false);
        fm.start();
    }

    public void addSyncPair(ISongResource songRes, File file) {
        file2SongRes.put(file, songRes);
        try {
            FileObject fo = VFS.getManager().resolveFile(file.getPath());
            fo2File.put(fo, file);
            fm.addFile(fo);
        } catch (FileSystemException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    private static void updateSongResFromFile (ISongResource songRes, File file) {
        String origPath = songRes.getFilePath();
        songRes.setFilePath(file.getPath());
        resHandler.importFromFileToDb(songRes);
        songRes.setFilePath(origPath);
        System.out.println("song resource updated: " + origPath);
    }

}
